package com.example.car.controller;

import com.example.car.entity.Role;
import com.example.car.entity.Router;
import com.example.car.entity.User;
import com.example.car.service.UserService;
import com.example.car.utils.JwtUtil;
import com.example.car.vo.CommonResult;
import com.example.car.vo.UserVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "登录接口")
@RestController
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    @ApiOperation("登录")
    public CommonResult login(@RequestBody User user) {

        User loginUser = userService.login(user);
        log.info("登录用户:{}", loginUser);
        if(loginUser == null) {
            return CommonResult.error("用户不存在");
        }
        Role role = userService.getUserRoleByUserId(loginUser.getId());
        String token = JwtUtil.getToken(loginUser.getId(), role);
        return CommonResult.success().data("data", token);
    }

    @PostMapping("register")
    @ApiOperation("注册")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult register(@RequestBody User user) {
        String phone = user.getPhone();
        String account = user.getAccount();

        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(account)) {
            return CommonResult.error("电话或账号不能为空！");
        }

        String result = userService.checkPhoneAndAccount(phone, account);

        if (result != null) {
            return CommonResult.error(result);
        }
        boolean judge = userService.save(user);
        if (!judge) {
            return CommonResult.error("系统异常");
        }
        return CommonResult.success();
    }

    @PostMapping("userInfo")
    @ApiOperation("获取用户信息接口")
    public CommonResult getUserInfo(HttpServletRequest request) {

        boolean judge = JwtUtil.checkToken(request);
        if(!judge) {
            return CommonResult.error("token异常");
        }
        String userId = JwtUtil.getUserIdByToken(request);
        User user = userService.getUserByUserId(userId);
        if(user == null) {
            return CommonResult.error("用户不存在");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        userVo.setGender(user.getGender());

        // 获取路由
        String role = JwtUtil.getUserRoleByToken(request);

        List<Router> list = userService.getRouterAddr(role);
        log.info("数据为:{}",list);
        return CommonResult.success().data("data", userVo).data("router", list);
    }
}
